﻿Imports SecretaryToolData

Public Class frmEmergencyContact

	Dim initialFamilyHeadPublisherID As Long? = Nothing

	Private Sub frmEmergencyContact_Load(sender As Object, e As System.EventArgs) Handles Me.Load
		cmboFamilyHeads.DataSource = PublisherData.FamilyHeads.OrderBy(Function(x) x.FullName).ToList
		cmboFamilyHeads.ValueMember = Extensions.GetMemberInfo(Function(x As PublisherData) x.PublisherID).Name
		cmboFamilyHeads.DisplayMember = Extensions.GetMemberInfo(Function(x As PublisherData) x.FullName).Name

		If initialFamilyHeadPublisherID.HasValue Then
			cmboFamilyHeads.SelectedValue = initialFamilyHeadPublisherID
		End If

	End Sub

	Private Sub cmboFamilyHeads_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cmboFamilyHeads.SelectedIndexChanged
		Try
			EmergencyContactInformationBindingSource.DataSource = New VM_EmergencyContactInformation(cmboFamilyHeads.SelectedValue)
			EmergencyContactInformationBindingSource.ResetBindings(False)
		Catch ex As Exception
			'the valuemember isn't set yet, so the lookup is invalid
		End Try

	End Sub

	Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
		Me.Close()
	End Sub

	Private Sub btnReport_Click(sender As System.Object, e As System.EventArgs) Handles btnReport.Click
		Dim reportLocation As String = Reports.EmergencyContactInformation(cmboFamilyHeads.SelectedValue).FullName
		If MessageBox.Show(String.Format("Report generated and saved at{0}{1}.{0}{0}Do you want to open it?", Environment.NewLine, reportLocation), "Report Successfully Generated", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
			Process.Start(reportLocation)
		End If
	End Sub

	Public Sub New()

		' This call is required by the designer.
		InitializeComponent()

	End Sub

	Public Sub New(FamilyHeadPublisherID As Long)

		' This call is required by the designer.
		InitializeComponent()

		initialFamilyHeadPublisherID = FamilyHeadPublisherID
	End Sub

	Private Sub btnReportAll_Click(sender As System.Object, e As System.EventArgs) Handles btnReportAll.Click
		Dim listFamilyHeads As New List(Of Long)

		'For Each id As PublisherData In PublisherData.FamilyHeads.OrderBy(Function(x) x.FullName).ToList
		For Each id As VM_PublisherInformation In VM_PublisherInformation.AllFamilyHeads
			listFamilyHeads.Add(id.PublisherID)
		Next
		Dim reportLocation As String = Reports.EmergencyContactInformation(listFamilyHeads).FullName
		If MessageBox.Show(String.Format("Report generated and saved at{0}{1}.{0}{0}Do you want to open it?", Environment.NewLine, reportLocation), "Report Successfully Generated", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
			Process.Start(reportLocation)
		End If

	End Sub

	Private Sub btnReportAllEmail_Click(sender As System.Object, e As System.EventArgs) Handles btnReportAllEmail.Click
		Dim reportLocation As String

		For Each id As VM_PublisherInformation In VM_PublisherInformation.AllFamilyHeads
			Dim newMessage As New MailToLinkBuilder

			reportLocation = Reports.EmergencyContactInformation(id.FamilyHeadID).FullName

			newMessage.Subject = "Emergency Contact Information"
			newMessage.Body = String.Format("{1},{0}{0}Please confirm your emergency contact information. If there are changes, please reply to this email and let me know so that the congregation records can be updated. If there are no changes, please reply to this email saying so.{0}{0}Thanks!{0}{0}Your Brother,{0}Congregation Secretary", Environment.NewLine, id.FullName)
			newMessage.Attachment = New IO.FileInfo(reportLocation)
			If id.Email IsNot Nothing Then
				newMessage.ToAddresses.Add(id.Email)
			Else

				newMessage.ToAddresses.Add(New VM_PublisherInformation(id.GroupOverseerID).Email)
				newMessage.ToAddresses.Add(New VM_PublisherInformation(id.GroupAssistantID).Email)
			End If

			Process.Start(newMessage.GetMailToLink())
		Next

		Process.Start(New IO.FileInfo(reportLocation).DirectoryName)

	End Sub
End Class